package org.tools;

import org.callTools.CallBackApi;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.pojo.MenuData;

import java.util.HashMap;
import java.util.Map;

import static org.tools.SdData.textCenter;

public class SdSearchData {
    public static Map<String, String> map = new HashMap<String, String>();
    static {
        map.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36");
        map.put("content-type","application/x-www-form-urlencoded");
        map.put("accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
    }
    public static void initUrl(int page, String className,String keyword, CallBackApi callBackApi){
        try {

            Document doc = Jsoup.connect("https://aitool.ai/page/"+page+"?s="+keyword+"&post_type=book").headers(map).get();

            Elements links = doc.getElementsByClass("card-book list-item");
            Elements title =doc.getElementsByClass("list-subtitle text-muted text-xs overflowClip_1");


            Elements paGesSize = doc.getElementsByClass("page-numbers");
            int pageSize = Integer.parseInt(paGesSize.get(paGesSize.size()-1).html().replaceAll(",",""));
            Elements tourl = doc.getElementsByClass("media-content");

            for(int i = 0; i < links.size(); i++){
                MenuData menuData = new MenuData();
                menuData.setMaxPage(pageSize);
                menuData.setToUrl(tourl.get(i).attr("href"));
                menuData.setClassName(className);
                menuData.setPages(page);
                menuData.setNameUrl(title.get(i).html());
                menuData.setImgUrl(textCenter(tourl.get(i).attr("data-bg"),"url(",")"));
                menuData.setDownLoad(menuData.getToUrl());
                callBackApi.data(menuData);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
